{extend name="admin@index_layout"/}
{block name="main"}
<link rel="stylesheet" href="__STATIC__/admin/css/cms.css">
<div class="layui-card">
    <div class="layui-card-body layui-form-pane">
        <form class="layui-form" method="post">
            <div class="layui-col-md8">
                <div class="layui-card">
                    <div class="layui-form-item">
                        <label class="layui-form-label">源语言站</label>
                        <div class="layui-input-block">
                            <input type="radio" name="from" value="auto" title="{$masterName}" checked>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">一键全选</label>
                        <div class="layui-input-block">
                            <input lay-skin="primary" type="checkbox" id="checkall" name="checkall" lay-filter="allChoose" value=""/><span>全选</span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">栏目推送</label>
                        <div class="layui-input-block check-box">
                            {volist name="sites" id="vo" empty="<div class='layui-tips'>请先建站点</div>"}
                            <input type="checkbox" name="sites[]" data-type="cat" data-push="{if $check_site && in_array($vo.id,$check_site)}1{/if}" value="{$vo.id}" lay-skin="primary" title="{$vo.name}{if $check_site && in_array($vo.id,$check_site)}[已推]{/if}" {if $check_site && in_array($vo.id,$check_site)}checked{/if}>
                            {/volist}
                        </div>
                    </div>
                    {eq name="type" value="1"}
                    <div class="layui-form-item">
                        <label class="layui-form-label">内容推送</label>
                        <div class="layui-input-block check-box">
                            {volist name="sites" id="vo" empty="<div class='layui-tips'>请先建站点</div>"}
                            <input type="checkbox" name="sites[]" data-type="page" data-push="{if $check_page_site && in_array($vo.id,$check_page_site)}1{/if}" value="{$vo.id}" lay-skin="primary" title="{$vo.name}{if $check_page_site && in_array($vo.id,$check_page_site)}[已推]{/if}" {if $check_page_site && in_array($vo.id,$check_page_site)}checked{/if}>
                            {/volist}
                        </div>
                    </div>
                    {/eq}
                    <div class="layui-form-item">
                        <label class="layui-form-label">重新推送</label>
                        <div class="layui-input-block">
                            <input type="radio" name="status" value="1" title="是">
                            <input type="radio" name="status" value="0" title="否" checked>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">修改时间</label>
                        <div class="layui-input-block layui-text">
                            2021-09-26
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">推送时间</label>
                        <div class="layui-input-block layui-text">
                            2021-09-26
                        </div>
                    </div>

                </div>
                {include file="cms@item_push" /}
            </div>

            <div class="layui-form-item layer-footer">
                <div class="layui-input-block">
                    <input type="hidden" name="id" value="{$catid}">
                    <button type="submit" class="send-btn layui-btn iconfont icon-send" lay-submit lay-filter="sub"> 推送并翻译</button>
                    <span class="btn-close" style="display: none">
                        <button type="submit" class="layui-btn layui-btn-danger iconfont icon-close" lay-submit lay-filter="close"> 关闭</button>
                    </span>
                </div>
            </div>
        </form>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
    layui.use(['form','yznForm','yzn','element'], function() {
        var yznForm = layui.yznForm;
        var form = layui.form;
        var element = layui.element;
        var yzn = layui.yzn;
        var push_list = [];
        //点击全选, 勾选
        form.on('checkbox(allChoose)', function (data) {
            var child = $(".check-box input[type='checkbox']");
            child.each(function (index, item) {
                item.checked = data.elem.checked;
            });
            form.render('checkbox');
        });
        form.on('submit(sub)', function(data) {
            check_push();
            if (push_list.length<=0){
                return false;
            }
            $('.send-btn').removeClass(' iconfont icon-send').addClass('layui-btn-disabled').prop('disabled', true).html('<i class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i> 正在翻译推送中...！千万不要关闭页面！');
            start_change(data.field);
            return false;
        });

        form.on('submit(close)', function(data) {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index)
            window.parent.location.reload();
        });

        function check_push(){
            push_list = [];
            var status = $("input[name='status']:checked").val();
            $("input[name='sites[]']:checked").each(function () {
                var push = $(this).attr('data-push');
                if (push==1 && status==0){
                    //无操作
                }else{
                    push_list.push({site_id:this.value,data_type:$(this).attr('data-type'),curr_dom:$(this)});
                }
            })
        }
        function start_change(field) {
            if (push_list.length<=0){
                $('.send-btn').attr('disabled', false).removeClass("layui-btn-disabled").addClass('layui-btn-normal iconfont icon-send') .html(' 重新推送');
                $('.btn-close').show();
                return false;
            }
            var site_id = push_list[0].site_id;
            var data_type = push_list[0].data_type;
            var curr_dom = push_list[0].curr_dom;
            curr_dom.next('div').after('<i id="push-loading" class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i>');
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "{:url('category')}",
                data: {id:field.id,site_id:site_id,type:data_type},
                success: function(res) {
                    push_list.shift();
                    $('#push-loading').remove();
                    start_change(field);
                    if (res.code == 1) {
                        curr_dom.next('div').find('span').text(res.data.info);
                        curr_dom.attr('data-push',1);
                        layer.msg(res.msg, { icon: 1 });
                    } else {
                        layer.msg(res.msg, { icon: 2 });
                    }
                },
                error: function(xhr, textstatus, thrown) {

                }
            });
        }
    });
</script>
{/block}

